package com.css.bjdt_report.dataSetManage.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.css.bjdt_report.dataSetManage.entity.DataSet;
import com.css.bjdt_report.dataSetManage.service.DataItemService;
import com.css.bjdt_report.dataSetManage.service.DataSetService;
import com.css.bjdt_report.util.CommonUtil;
import com.css.bjdt_report.util.PropertyFilter;

@Controller
@RequestMapping(value = "/set")
public class DataSetController {

	@Autowired
	private DataSetService dataSetService;

	@Autowired
	private DataItemService dataItemService;

	@RequestMapping("/list")
	public String list() {

		return "/dataset/list";
	}

	@RequestMapping("/input")
	public String input() {
		return "/dataset/input";
	}

	@ResponseBody
	@RequestMapping("/tableData")
	public Map tableData(HttpServletRequest request, @RequestParam(value = "pageNum") int pageNum,
			@RequestParam(value = "pageSize") int pageSize) {
		Criteria cnd = PropertyFilter.cri(request);
		cnd.getOrderBy().asc("TABLE_NAME");
		return dataSetService.queryPage(cnd, pageNum, pageSize);
	}

	//修改信息集
	@RequestMapping("/update")
	public String updateSet(DataSet dataSet, Model model) {
		dataSetService.update(dataSet);
		model.addAttribute("message", "操作成功");
		return "/dataset/input";
	}

	//编辑信息集
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editSet(@PathVariable String id, Model model) {
		model.addAttribute(dataSetService.querySetById(id));
		return "/dataset/input";
	}

	//保存信息集
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insertSet(DataSet dataSet, Model model) {
		dataSet.setId(CommonUtil.getUUID());
		dataSetService.insert(dataSet);
		model.addAttribute("message", "操作成功");
		return "/dataset/input";
	}

	//删除一个信息集
	@ResponseBody
	@RequestMapping(value = "/delete/{id}")
	public String deleteSet(@PathVariable String id) {
		dataItemService.deleteItem(id);
		dataSetService.delete(id);
		return "success";
	}

}
